package com.pphh.demo;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * Please add description here.
 *
 * @author huangyinhuang
 * @date 2019/4/22
 */
public class DemoReactor {

    /**
     * mono + flux
     * just/fromArray/empty/error/never/interval/intervalMillis
     * range
     * merge
     * map/flatmap/filter/zip
     * subscribe
     * doOnNex/doOnComplete
     * generate/create
     * buffer/bufferTimeout
     * window
     * zip/zipWith
     * reduce/reduceWith
     * merge/mergeSequential
     * flatMap 和 flatMapSequential
     * concatMap
     * combineLatest
     * onErrorReturn/switchOnError/onErrorResumeWith
     * publishOn
     */
    public static void testSimple() {
        Flux.just("Hello, world").subscribe(System.out::println);
        Flux.fromArray(new String[]{"a", "b", "c"}).subscribe(System.out::println);
    }

    public static void testSimple2() {

        Flux.range(1, 3).subscribe(i -> System.out.println(i));

        Flux<Integer> ints = Flux.range(1, 4)
                .map(i -> {
                    if (i <= 3) return i;
                    throw new RuntimeException("Got to 4");
                });
        ints.subscribe(i -> System.out.println(i),
                error -> System.err.println("Error: " + error));
    }

    public static void testMono() {
        Mono<String> data = Mono.just("foo");
        data.subscribe(i -> System.out.println(i));
    }

    public static void testGenerate() {
        Flux<String> flux = Flux.generate(
                () -> 0,
                (state, sink) -> {
                    sink.next("3 x " + state + " = " + 3 * state);
                    if (state == 10) sink.complete();
                    return state + 1;
                });

        flux.subscribe(i -> System.out.println(i));
    }

}
